﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.Urls;
using SubSonic;

namespace Medianamik.Client.Data.Repositories
{
    public class UrlRepository : IUrlRepository
    {
        private readonly IMedianamikRepository<BaseWebType> _baseWebTypeRepository;

        public UrlRepository(IMedianamikRepository<BaseWebType> baseWebTypeRepository)
        {
            _baseWebTypeRepository = baseWebTypeRepository;
        }

        public IMedianamikRepository<BaseWebType> BaseWebTypeRepository
        {
            get { return _baseWebTypeRepository; }
        }

        public List<UrlXmlDoc> GetUrlXmlDocs()
        {
            return BaseWebTypeRepository.AlllInstancesWithFilters(ContentState.All, ActiveStatus.Activated,
                null, null, null, null, "generation desc", null, true).Select(record => new UrlXmlDoc(record.NodeId,
                    record.TypeId, record.ParentNodeId, record.Path, record.CreatedOn, record.Position,
                    record.Depth, false, record.Culture, record.Revision, record.Generation, record.Url)).ToList();
        }

        public List<UrlXmlDoc> GetUrlXmlDocs(Guid pNodeId)
        {
            return BaseWebTypeRepository.AlllInstancesWithFilters(ContentState.All, ActiveStatus.Activated,
                null, null, new[] { new Where { ColumnName = BaseWebType.Columns.NodeId, ParameterValue = pNodeId } },
                null, "generation desc", null, true).Select(record =>
                    new UrlXmlDoc(record.NodeId, record.TypeId, record.ParentNodeId, record.Path, record.CreatedOn,
                        record.Position, record.Depth, false, record.Culture, record.Revision, record.Generation,
                        record.Url)).ToList();
        }
    }
}